home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / intrvews / xgrab.lha / xgrab / ui / GC / README < prev    next >
Encoding:
Text File  |  1990-04-25  |  14.7 KB  |  313 lines

  1. Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
  2. This material may be freely distributed, provided this notice is retained.
  3. This material is provided as is, with no warranty expressed or implied.
  4. Use at your own risk.
  5.  
  6. This is version 1.6.
  7.  
  8. HISTORY -
  9.  
  10.   This collector was developed as a part of research projects supported in
  11. part by the National Science Foundation and the Defense Advance Research
  12. Projects Agency.  The SPARC specific code was contributed by Mark Weiser
  13. (weiser.pa@xerox.com).  The Encore Multimax modifications were supplied by
  14. Kevin Kenny (kenny@m.cs.uiuc.edu).  The adaptation to the RT is largely due
  15. to Vernon Lee (scorpion@rice.edu), on machines made available by IBM.
  16. The HP specific code and a number of good suggestions for improving the
  17. generic code are due to Walter Underwood (wunder@hp-ses.sde.hp.com).
  18. (Blame for misinstallation of those modifications goes to the first author,
  19. however.) Some of the improvements incorporated in this version were
  20. suggested by David Chase at Olivetti Research.
  21.  
  22.   This is intended to be a general purpose, garbage collecting storage
  23. allocator.  The algorithms used are described in:
  24.  
  25. Boehm, H., and M. Weiser, "Garbage Collection in an Uncooperative Environment",
  26. Software Practice & Experience, September 1988, pp. 807-820.
  27.  
  28.   Many of the ideas underlying the collector have previously been explored
  29. by others.  (We discovered recently that Doug McIlroy wrote a more or less
  30. similar collector that is part of version 8 UNIX (tm).)  However none of this
  31. work appears to have been widely disseminated.
  32.  
  33.   The tools for detecting storage leaks described in the above paper
  34. are not included here.  There is some hope that they might be released
  35. by Xerox in the future.
  36.  
  37.  
  38. GENERAL DESCRIPTION
  39.  
  40.   Since the collector does not require pointers to be tagged, it does not
  41. attempt to insure that all inaccessible storage is reclaimed.  However,
  42. in our experience, it is typically more successful at reclaiming unused
  43. memory than most C programs using explicit deallocation.
  44.  
  45.   In the following, an "object" is defined to be a region of memory allocated
  46. by the routines described below.  
  47.  
  48.   Any objects not intended to be collected must be pointed to either
  49. from other such accessible objects, or from the registers,
  50. stack, data, or statically allocated bss segments.  It is usually assumed
  51. that all such pointers point to the beginning of the object.  (This does
  52. not disallow interior pointers; it simply requires that there must be a
  53. pointer to the beginning of every accessible object, in addition to any
  54. interior pointers.  Conditionally compiled code to check for pointers to the
  55. interiors of objects is supplied.  As explained in "gc.h", this
  56. may create other problems, however.)
  57.  
  58.   Note that pointers inside memory allocated by the standard "malloc" are not
  59. seen by the garbage collector.  Thus objects pointed to only from such a
  60. region may be prematurely deallocated.  It is thus suggested that the
  61. standard "malloc" be used only for memory regions, such as I/O buffers, that
  62. are guaranteed not to contain pointers.  Pointers in C language automatic,
  63. static, or register variables, are correctly recognized.
  64.  
  65.   The collector does not understand SunOS 4.x dynamic libraries.  Space
  66. allocated by the dynamic linker past at addresses higher than "_end" will not
  67. be seen by the collector.  (We have not had a chance to track down exactly
  68. what ends up there.  Some data does.  If we understood exactly where things
  69. ended up, it would probably be easy to fix this problem.)  When in doubt,
  70. use -Bstatic.
  71.  
  72.   The collector is designed to minimize stack growth if list-like structures
  73. store the link in their first field; for example
  74.  
  75.   struct list_node {
  76.       struct list_node * link; /* first field */
  77.       ...
  78.       };
  79.   
  80. instead of
  81.  
  82.   struct list_node {
  83.       ...
  84.       struct list_node * link; /* last field */
  85.       };
  86.  
  87.   This should not matter for lists that are less than tens of thousands
  88. of elements long.
  89.  
  90.   Signal processing for most signals is deferred during collection. (The
  91. necessary calls to sigsetmask may need to be commented out under a pure
  92. system V implementation, since there does not seem to be an equivalent
  93. call.  Multiple calls to signal are likely to be slow.)
  94.  
  95. INSTALLATION AND PORTABILITY
  96.  
  97.   As distributed, the collector produces garbage collection statistics
  98. during every collection.  Once the collector is known to operate properly,
  99. these can be suppressed by defining the macro SILENT at the top
  100. of "gc.h".  (The given statistics exhibit a few peculiarities.
  101. Things don't appear to add up for a variety of reasons, most notably
  102. fragmentation losses.  These are probably much more significant for the
  103. contrived program "test.c" than for your application.)
  104.  
  105.   Note that typing "make test" will automatically compare the output
  106. of the test program against the correct output.  This does require that
  107. collection statistics have been disabled.
  108.  
  109.   The Makefile will generate a library gc.a which you should link against.
  110. It is suggested that if you need to replace a piece of the collector
  111. (e.g. mark_roots.c) you simply list your version ahead of gc.a on the
  112. ld command line, rather than replacing the one in gc.a.
  113.  
  114.   The collector currently is designed to run essentially unmodified on
  115. the following machines:
  116.  
  117.         Sun 3
  118.         Sun 4  (except under some versions of 3.2)
  119.         Vax under Berkeley UNIX
  120.         Sequent Symmetry  (no concurrency)
  121.         Encore Multimax   (no concurrency)
  122.         MIPS M/120 (and presumably M/2000) (RISC/os 4.0 with BSD libraries)
  123.         IBM PC/RT  (Berkeley UNIX)
  124.         HP9000/300
  125.  
  126.   For these machines you should check the beginning of gc.h
  127. to verify that the machine type is correctly defined.  On an Encore Multimax,
  128. MIPS M/120, or a PC/RT, you will also need to make changes to the
  129. Makefile, as described by comments there.
  130.  
  131.   In all cases we assume that pointer alignment is consistent with that
  132. enforced by the standard C compilers.  If you use a nonstandard compiler
  133. you may have to adjust the alignment parameters defined in gc.h.
  134.  
  135.   On a MIPS machine or PC/RT, we assume that no calls to sbrk occur during a
  136. collection. (This is necessary due to the way stack expansion works on these
  137. machines.) This may become false if certain kinds of I/O calls are inserted
  138. into the collector.
  139.  
  140.   For machines not already mentioned, or for nonstandard compilers, the
  141. following are likely to require change:
  142.  
  143. 1.  The parameters at the top of gc.h and the definition of
  144.     TMP_POINTER_MASK further down in the same file.
  145.  
  146. 2.  mach_dep.c.
  147.       The most important routine here is one to mark from registers.
  148.     The distributed file includes a generic hack (based on setjmp) that
  149.     happens to work on many machines, and may work on yours.  Try
  150.     compiling and running setjmp_test.c to see whether it has a chance of
  151.     working.  (This is not correct C, so don't blame your compiler if it
  152.     doesn't work.  Based on limited experience, register window machines
  153.     are likely to cause trouble.  If your version of setjmp claims that
  154.     all accessible variables, including registers, have the value they
  155.     had at the time of the longjmp, it also will not work.  Vanilla 4.2 BSD
  156.     makes such a claim.  SunOS does not.)
  157.       This file also contains interface routines that save registers
  158.     not normally preserved by the C compiler.  These are intended for
  159.     a fast assembly language interface to the allocator, such as the
  160.     one that is used by the Russell compiler.  (These routines work
  161.     only for small objects.  A call to one of these routines ensures
  162.     that the free list for a particular object size is nonempty.  Normally
  163.     in-line code would call these routines only after finding an empty free
  164.     list for an about-to-be-allocated object size.)  If a pure C interface
  165.     is used, these routines are not needed.
  166.       If your machine does not allow in-line assembly code, or if you prefer
  167.     not to use such a facility, mach_dep.c may be replaced by a .s file
  168.     (as we did for the MIPS machine and the PC/RT).
  169.  
  170. 3.  mark_roots.c.
  171.       These are the top level mark routines that determine which sections
  172.     of memory the collector should mark from.  This is normally not
  173.     architecture specific (aside from the macros defined in gc.h and
  174.     referenced here), but it can be programming language and compiler
  175.     specific.  The supplied routine should work for most C compilers
  176.     running under UNIX.
  177.  
  178. 4.  The sigsetmask call does not appear to exist under system V UNIX.
  179.     It is used by the collector to block and unblock signals at times at
  180.     which an asynchronous allocation inside a signal handler could not
  181.     be tolerated.  Under system V, it is possible to remove these calls,
  182.     provided no storage allocation is done by signal handlers.  The
  183.     alternative is to issue a sequence of system V system calls, one per
  184.     signal that is actually used.  This may be a bit slow.
  185.  
  186.   For a different versions of Berkeley UN*X or different machines using the
  187. Motorola 68000, Vax, SPARC, 80386, NS 32000, PC/RT, or MIPS architecture,
  188. it should frequently suffice to change definitions in gc.h.
  189.  
  190.  
  191. THE C INTERFACE TO THE ALLOCATOR
  192.  
  193.   The following routines are intended to be directly called by the user.
  194. Note that only gc_malloc and gc_init are necessary.  Gc_realloc is provided
  195. for applications that already use realloc.  The remaining routines are used
  196. solely to enhance performance.  It is suggested that they be used only after
  197. initial debugging.
  198.  
  199. 1)  gc_init()
  200.     - called once before allocation to initialize the collector.
  201.  
  202. 2)  gc_malloc(nbytes)
  203.     - allocate an object of size nbytes.  Unlike malloc, the object is
  204.       cleared before being returned to the user.  (For even better performance,
  205.       it may help to expand the relevant part of gc_malloc in line.
  206.       This is done by the Russell compiler, for example.)  Gc_malloc will
  207.       invoke the garbage collector when it determines this to be appropriate.
  208.       (A number of previous collector bugs resulted in objects not getting
  209.       completely cleared.  We claim these are all fixed.  But if you encounter
  210.       problems, this is a likely source to check for.  The collector tries
  211.       hard to avoid clearing any words that it doesn't have to.  Thus this
  212.       is a bit subtle.)  Gc_malloc fails (generates a segmentation fault)
  213.       if it is called with a 0 argument.
  214.  
  215. 3)  gc_malloc_atomic(nbytes)
  216.     - allocate an object of size nbytes that is guaranteed not to contain any
  217.       pointers.  The returned object is not guaranteed to be cleeared.
  218.       (Can always be replaced by gc_malloc, but results in faster collection
  219.       times.  The collector will probably run faster if large character
  220.       arrays, etc. are allocated with gc_malloc_atomic than if they are
  221.       statically allocated.)
  222.  
  223. 4)  gc_realloc(object, new_size)
  224.     - change the size of object to be new_size.  Returns a pointer to the
  225.       new object, which may, or may not, be the same as the pointer to
  226.       the old object.  The new object is taken to be atomic iff the old one
  227.       was.  If the new object is composite and larger than the original object,
  228.       then the newly added bytes are cleared (we hope).  This is very likely
  229.       to allocate a new object, unless MERGE_SIZES is defined in gc.h.
  230.       Even then, it is likely to recycle the old object only if the object
  231.       is grown in small additive increments (which, we claim, is generally bad
  232.       coding practice.)
  233.  
  234. 5)  gc_free(object)
  235.     - explicitly deallocate an object returned by gc_malloc or
  236.       gc_malloc_atomic.  Not necessary, but can be used to minimize
  237.       collections if performance is critical.
  238.  
  239. 6)  expand_hp(number_of_4K_blocks)
  240.     - Explicitly increase the heap size.  (This is normally done automatically
  241.       if a garbage collection failed to reclaim enough memory.  Explicit
  242.       calls to expand_hp may prevent unnecessarily frequent collections at
  243.       program startup.)
  244.  
  245.   The global variable dont_gc can be set to a non-zero value to inhibit
  246. collections, e.g. during a time-critical section of code.  (This may cause
  247. otherwise unnecessary expansion of the process' memory.)
  248.  
  249.   The variable non_gc_bytes, which is normally 0, may be changed to reflect
  250. the amount of memory allocated by the above routines that should not be
  251. considered as a candidate for collection.  Collections are inhibited
  252. if this exceeds a given fraction (currently 3/4) of the total heap size.
  253. The heap is simply expanded instead.  Careless use may, of course, result
  254. in excessive memory consumption.
  255.  
  256.   Some additional tuning is possible through the parameters defined
  257. near the top of gc.h.
  258.   
  259.   The two gc_malloc routines may be declared to return a suitable pointer
  260. type.  It is not intended that gc.h be included by the user program.
  261. If only gc_malloc is intended to be used, it might be appropriate to define:
  262.  
  263. #define malloc(n) gc_malloc(n)
  264. #define calloc(m,n) gc_malloc((m)*(n))
  265.  
  266.   More complete emulations of the standard C allocation routines are
  267. contained and described in "interface.c" (contributed by David Chase).
  268.  
  269.   No attempt is made to use obscure names for garbage collector routines
  270. and data structures.  Name conflicts are possible.  (Running "nm gc.a"
  271. should identify names to be avoided.)
  272.  
  273.  
  274. ASSEMBLY LANGUAGE INTERFACE
  275.  
  276.   There is a provision for a very fast assembly language and/or in-line
  277. C interface.  See the beginning comments in alloc.c.  On some architectures,
  278. additional code must be supplied near the beginning of mach_dep.c for
  279. this to work.  Using an assembly language interface, and partially
  280. expanding the allocation code in-line, most allocations will take on the
  281. order of 4 or 5 instructions each.  (Explicit deallocations can be kept
  282. down to something similar if the object is atomic and of known size.
  283. Note that in-line deallocation code for composite objects should clear
  284. the object before returning it to the appropriate free list.)
  285.  
  286.  
  287. BUGS
  288.  
  289.   Recently fixed bugs:
  290.  
  291.   Version 1.3 and immediately preceding versions contained spurious
  292. assembly language assignments to TMP_SP.  Only the assignment in the PC/RT
  293. code is necessary.  On other machines, with certain compiler options,
  294. the assignments can lead to an unsaved register being overwritten.
  295. Known to cause problems under SunOS 3.5 WITHOUT the -O option.  (With
  296. -O the compiler recognizes it as dead code.  It probably shouldn't,
  297. but that's another story.)
  298.  
  299.   Version 1.4 and earlier versions used compile time determined values
  300. for the stack base.  This no longer works on Sun 3s, since Sun 3/80s use
  301. a different stack base.  We now use a straightforward heuristic on all
  302. machines on which it is known to work (incl. Sun 3s) and compile-time
  303. determined values for the rest.  There should really be library calls
  304. to determine such values.
  305.  
  306.   Version 1.5 and earlier did not ensure 8 byte alignment for objects
  307. allocated on a sparc based machine.
  308.  
  309.   Please address bug reports to boehm@rice.edu.  If you are contemplating
  310. a major addition, you might also send mail to ask whether it's already
  311. been done.
  312.  
  313.